from flask import Blueprint, request
from flask_jwt_extended import create_access_token

from extension import db, cors

from models import User, Account

users_bp = Blueprint('users', __name__)


@users_bp.route('/login', methods=['POST'])
def login():
    username = request.form.get('username')
    password = request.form.get('password')
    user = User.query.filter_by(username=username, password=password).first()
    if user:
        account = Account.query.filter_by(username=username).first()
        admin = account.admin
        identity_info = {'username': username, 'admin': admin}
        access_token = create_access_token(identity=identity_info)
        #       print(jwt.verify_access_token(access_token))
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": access_token
        }

    else:
        return {
            "code": "1",
            "message": "ERROR"
        }


@users_bp.route('/register', methods=['POST'])
def register():
    username = request.json.get('username')
    password = request.json.get('password')
    phone = request.json.get('phone')
    address = request.json.get('address')
    firstname = request.json.get('firstname')
    lastname = request.json.get('lastname')
    admin = int(request.json.get('admin'))
    question = request.json.get('question')
    answer = request.json.get('answer')
    print('username',username)
    print('password',password)
    print('admin',admin)
    print('phone',phone)
    print('address',address)
    print('firstname',firstname)
    print('lastname',lastname)
    print('question',question)
    print('answer',answer)
    print(admin in [0,1,2])

    result = User.query.filter_by(username=username).first()
    if result:
        return {
            "code": "1",
            "message": "ERROR",
            "data": "用户名已存在"
        }

    account = Account(username=username, password=password, phone=phone, address=address, firstname=firstname,
                      lastname=lastname, admin=admin, question=question, answer=answer, money=0, borrowBookNum=0)
    user = User(username=username, password=password)
    if account and admin in [0, 1, 2] and username != '' and password != '' and phone != '' and address != '' \
            and firstname != '' and lastname != '' and question != '' and answer != '':
        db.session.add(account)
        db.session.add(user)
        db.session.commit()
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": "注册成功"
        }
    else:
        return {
            "code": "1",
            "message": "ERROR",
            "data": "注册失败"
        }


@users_bp.route('/<string:username>', methods=['GET'])
def getUserInfo(username):
    user = Account.query.filter_by(username=username).first()
    if user:
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": {
                "username": user.username,
                "phone": user.phone,
                "address": user.address,
                "firstname": user.firstname,
                "lastname": user.lastname,
                "question": user.question,
                "answer": user.answer,
                "money": user.money,
                "borrowNumber": user.borrowBookNum
            }
        }
    else:
        return {
            "code": "1",
            "message": "ERROR"
        }


@users_bp.route('/updateInfo/<string:username>', methods=['PUT'])
def updateInfo(username):
    newUsername = request.json.get('username')
    password = request.json.get('password')
    phone = request.json.get('phone')
    address = request.json.get('address')
    firstname = request.json.get('firstname')
    lastname = request.json.get('lastname')
    question = request.json.get('question')
    answer = request.json.get('answer')

    user = Account.query.filter_by(username=username).first()
    loginUser = User.query.filter_by(username=username).first()
    if user and newUsername != '' and password != '' and phone != '' and address != '' \
            and firstname != '' and lastname != '' and question != '' and answer != '' :
        user.username = newUsername
        user.password = password
        user.phone = phone
        user.address = address
        user.firstname = firstname
        user.lastname = lastname
        user.question = question
        user.answer = answer
        loginUser.username = newUsername
        loginUser.password = password
        db.session.commit()
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": "更新用户信息成功"
        }
    else:
        return {
            "code": "1",
            "message": "ERROR",
            "data": "更新用户信息失败"
        }


@users_bp.route('/getquestion/<string:username>', methods=['GET'])
def getQuestion(username):
    user = Account.query.filter_by(username=username).first()
    if user:
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": user.question
        }
    else:
        return {
            "code": "1",
            "message": "ERROR",
            "data":""
        }


@users_bp.route('/verifyAnswer/<string:username>', methods=['POST'])
def verifyAnswer(username):
    answer = request.form.get('answer')
    user = Account.query.filter_by(username=username).first()
    if user and answer == user.answer:
        return {
            "code": "0",
            "message": "SUCCESS"
        }
    else:
        return {
            "code": "1",
            "message": "ERROR"
        }


@users_bp.route('/updatePwd/<string:username>', methods=['POST'])
def updatePwd(username):
    password = request.json.get('password')
    answer = request.json.get('answer')
    account = Account.query.filter_by(username=username, answer=answer).first()
    if account:
        user = User.query.filter_by(username=username).first()
        account.password = password
        user.password = password
        db.session.commit()
        return {
            "code": "0",
            "message": "SUCCESS"
        }
    else:
        return {
            "code": "1",
            "message": "ERROR"
        }


@users_bp.route('/admin/<string:username>', methods=['GET'])
def getAdmin(username):
    user = Account.query.filter_by(username=username).first()
    if user:
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": user.admin
        }
    else:
        return {
            "code": "1",
            "message": "ERROR"
        }


@users_bp.route('/isExist', methods=['GET'])
def isExist():
    username = request.args.get('username')
    user = Account.query.filter_by(username=username).first()
    if user:
        return {
            "code": "0",
            "message": "SUCCESS",
            "data": "exist"
        }
    else:
        return {
            "code": "0",
            "message": "ERROR",
            "data": "no exist"
        }


@users_bp.route('/addMoney/<string:username>/<int:money>', methods=['GET'])
def addMoney(username, money):
    user = Account.query.filter_by(username=username).first()
    user.money += money
    db.session.commit()
    return {
        "code": "0",
        "message": "SUCCESS"
    }